<?php

$archived=0;
$archivedate = date("Y-m-d");
$archivedate = strtotime(date("Y-m-d", strtotime($archivedate)) . " -6 month");
$archivedate = date("Y-m-d",$archivedate);
list($day, $month, $year,$spare) = preg_split('[/]', $archivedate);
$newdate=$year.'-'.$month.'-'.$day;
$newdate=$year.''.$month.''.$day;
$archivedate=strtotime($newdate);

$query1=mysql_query("SELECT * FROM tbljobs WHERE Closed='checked'");
while($row = mysql_fetch_array($query1))
{
list($day, $month, $year,$spare) = preg_split('[/]', $row[Date]);
$jobdate='20'.$year.'-'.$month.'-'.$day;
$jobdate=strtotime($jobdate);

if($jobdate<=$archivedate)
 {
$query="INSERT INTO tbljobsarchive SELECT * FROM tbljobs WHERE LogNumber='$row[LogNumber]'";
mysql_query($query);
$query="DELETE FROM tbljobs WHERE LogNumber='$row[LogNumber]'";
mysql_query($query);
$query="INSERT INTO tblsolutionsarchive SELECT * FROM tblsolutions WHERE LogNumber='$row[LogNumber]'";
mysql_query($query);
$query="DELETE FROM tblsolutions WHERE LogNumber='$row[LogNumber]'";
mysql_query($query);
#$body .='<tr><td>Archived '.$row[LogNumber].'</td></tr>';
$archived++;
}
}
$query='OPTIMIZE TABLE `tbljobs` , `tblsolutions` ';
mysql_query($query);
$body .= '<tr><td>Archived '.$archived.' Jobs</td></tr>';

?>
